﻿Imports System.Collections.Specialized

Public Class Site

    Private _歌曲列表 As New DataTable
    Private _歌词列表 As New DataTable

    Public Overrides Sub Init()
        MyBase.Init()
        _歌曲列表.Columns.Add("作者")
        _歌曲列表.Columns.Add("歌曲名")
        _歌曲列表.Columns.Add("歌曲路径")

        _歌词列表.Columns.Add("作者")
        _歌词列表.Columns.Add("歌曲名")

        Dim dif As New IO.DirectoryInfo(IO.Path.Combine(Me.GetCurrentDirectory(), "Song"))
        Try
            If dif IsNot Nothing Then
                For Each fif As IO.FileInfo In dif.GetFiles("*.*")
                    Dim content() As String = IO.Path.GetFileNameWithoutExtension(fif.Name).Split(New String() {"-"}, StringSplitOptions.None)
                    If content.Length = 2 Then
                        _歌曲列表.Rows.Add(New String() {content(0).Trim(), content(1).Trim(), dif.FullName})
                    End If
                Next
            End If
        Catch ex As Exception

        End Try

        dif = New IO.DirectoryInfo(IO.Path.Combine(Me.GetCurrentDirectory(), "Lyrics"))
        Try
            If dif IsNot Nothing Then
                For Each fif As IO.FileInfo In dif.GetFiles("*.lrc")
                    Dim content() As String = IO.Path.GetFileNameWithoutExtension(fif.Name).Split(New String() {"-"}, StringSplitOptions.None)
                    If content.Length = 2 Then
                        _歌词列表.Rows.Add(New String() {content(0).Trim(), content(1).Trim()})
                    End If
                Next
            End If
        Catch ex As Exception

        End Try
    End Sub

#Region "创建歌曲数据源"
    Private Function CreateDataSourceSong() As DataTable
        Dim dt As DataTable
        Dim strSql_1 As String
        strSql_1 = "select ID as 编号,Songer as 歌手,Song as 歌曲,Memo as 备注,'' as 本地是否存在,'' as 是否存在歌词 from XSCSong  "
        dt = ct.FillTable(strSql_1)
        For Each dr As DataRow In dt.Rows
            dr("歌手") = Trim(dr("歌手"))
        Next

        dt.DefaultView.Sort = "歌手"
        dt = dt.DefaultView.ToTable

        Dim strContent As String = ""
        Dim strContents(1) As String
        Dim bExist As Boolean = False
        Dim bExist1 As Boolean = False
        For Each dr As DataRow In dt.Rows
            bExist = False
            bExist1 = False
            strContents(0) = dr("歌手").ToString
            strContents(1) = dr("歌曲").ToString
            dr("歌曲") = "    " & dr("歌曲").ToString.Trim
            If strContents.Length = 2 Then
                Dim drs() As DataRow = _歌曲列表.Select("作者='" + strContents(0).Trim + "' AND 歌曲名='" + strContents(1).Trim + "'")
                If drs.Length > 0 Then
                    bExist = True
                End If

                drs = _歌词列表.Select("作者='" + strContents(0).Trim + "' AND 歌曲名='" + strContents(1).Trim + "'")
                If drs.Length > 0 Then
                    bExist1 = True
                End If

            End If

            If dr("本地是否存在").ToString.Contains("本地不存在") OrElse dr("本地是否存在").ToString.Contains("歌词不存在") Then
                Continue For
            End If

            If bExist Then
                dr("本地是否存在") = "是"
            Else
                dr("本地是否存在") = "否"
            End If

            If bExist1 Then
                dr("是否存在歌词") = "是"
            Else
                dr("是否存在歌词") = "否"
            End If
        Next

        Return dt
    End Function
#End Region

    Private Sub PrintProcess(ByVal content As String)
        lbl1000.Text = content
        System.Windows.Forms.Application.DoEvents()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try

            Dim englishFolder As String = System.IO.Path.Combine(GetCurrentDirectory, "Report")
            If Not System.IO.Directory.Exists(englishFolder) Then
                System.IO.Directory.CreateDirectory(englishFolder)
            End If

            Dim englishPath As String = System.IO.Path.Combine(englishFolder, "Song.xls")
            Dim bCreate As Boolean = True
            If System.IO.File.Exists(englishPath) Then
                System.IO.File.Delete(englishPath)
            End If

            Dim dt As System.Data.DataTable = CreateDataSourceSong()
            If dt.Rows.Count = 0 Then
                ShowMessageBox("没有数据")
                Exit Sub
            End If

            For jndex As Integer = 0 To dt.Rows.Count - 2
                For kdex As Integer = jndex + 1 To dt.Rows.Count - 1
                    If dt.Rows(jndex)("歌手") = dt.Rows(kdex)("歌手") Then
                        dt.Rows(kdex)("歌手") = ""
                    End If
                Next
            Next

            Dim englishCreate As New SongOut(englishPath)
            englishCreate.报表记录条数字 = Me.报表记录条数字
            englishCreate.报表字体 = Me.报表字体

            AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

            englishCreate.DoOutData(dt)
            RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess

            '打开文件
            Using pro As New Process
                pro.StartInfo.FileName = englishPath
                pro.Start()
            End Using

        Catch ex As Exception
            ShowMessageBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

#Region "报表必须使用"
    Private _报表字体 As String = "宋体"
    Public Overrides Property 报表字体() As String
        Get
            Return _报表字体
        End Get
        Set(ByVal value As String)
            _报表字体 = value
        End Set
    End Property

    Private _报表记录条数字 As Integer = 55
    Public Overrides Property 报表记录条数字() As Integer
        Get
            Return _报表记录条数字
        End Get
        Set(ByVal value As Integer)
            _报表记录条数字 = value
        End Set
    End Property
#End Region
End Class
